//package com.example.demo.auth;
//
//import com.example.demo.common.Constant;
//import com.example.demo.dao.UserRepository;
//import com.example.demo.model.entity.UserModel;
//import com.example.demo.service.impl.UserService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.authentication.AuthenticationProvider;
//import org.springframework.security.authentication.BadCredentialsException;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.AuthenticationException;
//import org.springframework.security.core.GrantedAuthority;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.security.core.userdetails.UsernameNotFoundException;
//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
//import org.springframework.util.DigestUtils;
//
//import java.util.ArrayList;
//
///**
// * Created by Administrator on 2018/3/30.
// */
//public class CustomAuthenticationProvider implements AuthenticationProvider {
//
//    private UserDetailsService userDetailsService;
//
//    private BCryptPasswordEncoder bCryptPasswordEncoder;
//
//    public CustomAuthenticationProvider(UserDetailsService userDetailsService, BCryptPasswordEncoder bCryptPasswordEncoder) {
//        this.userDetailsService = userDetailsService;
//        this.bCryptPasswordEncoder = bCryptPasswordEncoder;
//    }
//
//    @Override
//    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
//        String name = authentication.getName();
//        String password = authentication.getCredentials().toString();
//        UserModel userDetails = (UserModel) userDetailsService.loadUserByUsername(name);
//        if (null != userDetails) {
//            String encodePassword = DigestUtils.md5DigestAsHex((password).getBytes());
//            if (userDetails.getPassword().equals(encodePassword)) {
//                ArrayList<GrantedAuthority> authorities = new ArrayList<>();
//                if (userDetails.getRole() == Constant.Role.USER.getRoleCode()) {
//                    authorities.add(new GrantedAuthorityImpl(Constant.Role.USER.getRoleName()));
//                } else {
//                    authorities.add(new GrantedAuthorityImpl(Constant.Role.ADMIN.getRoleName()));
//                }
//                Authentication auth = new UsernamePasswordAuthenticationToken(userDetails.getUserId(), userDetails.getRole(), authorities);
//                return auth;
//            } else {
//                throw new BadCredentialsException("Account or password error");
//            }
//        } else {
//            throw new UsernameNotFoundException("Account or password error");
//        }
//    }
//
//    /**
//     * 是否可以提供输入类型的认证服务
//     *
//     * @param authentication
//     * @return
//     */
//    @Override
//    public boolean supports(Class<?> authentication) {
//        return authentication.equals(UsernamePasswordAuthenticationToken.class);
//    }
//}
